<?php
/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2020/5/26 10:18
 */

namespace Eduxplus\WebsiteBundle\Controller\Web;

use Eduxplus\CoreBundle\Lib\Base\BaseHtmlController;
use Eduxplus\WebsiteBundle\Service\CategoryService;
use Eduxplus\WebsiteBundle\Service\GoodsService;
use Symfony\Component\HttpFoundation\Request;

class CourseController extends BaseHtmlController
{

    public function indexAction($categoryId,$isFree,Request $request, CategoryService $categoryService, GoodsService $goodsService){
        $page = $request->get("page");
        $page = $page?$page:1;
        $pageSize = 40;

        $brand = $categoryService->getBrands();
        list($pagination, $list) = $goodsService->getCategoryGoods($categoryId, $isFree, $page, $pageSize);
        $data = [];
        $data['categoryId'] = $categoryId;
        $data['brands'] = $brand;
        $data['id'] = $categoryId;
        $data['isFree'] = $isFree;
        $data['list'] = $list;
        $data['route'] = "app_course";
        $data['seoTitle']="课程";
        $data['seoDescr']="课程列表";
        $data['seoKw']=$categoryService->getAllCategoryNames();
        $data['pagination'] = $pagination;
        return $this->render("@WebsiteBundle/course/index.html.twig", $data);
    }
    
    public function detailAction($id, GoodsService $goodsService){
        $detail = $goodsService->getInfoById($id);
        if(!$detail || !$detail["status"]){
            return $this->render("@WebsiteBundle/404.html.twig");
        }

        $id = $detail['id'];
        $data = [];
        $data["firstId"] = $detail["firstCategoryId"];
        $data["info"] = $detail;
        $data['studyPlan'] = $goodsService->getStudyPlan($id);
        $uid = $this->getUid();
        $data['fav'] = [];
        if($uid){
            $data['fav'] = $goodsService->getFav($id, $uid);
        }
        $data['route'] = "app_course";
        $data['seoTitle']="课程";
        $data['seoDescr']=$detail["seoDescr"];
        $data['seoKw']=$detail["seoKeyWord"];
        
        return $this->render("@WebsiteBundle/course/detail.html.twig", $data);
    }
    
    public function doFavAction($id, GoodsService $goodsService){
        $uid = $this->getUid();
        $check = $goodsService->getFav($id, $uid);
        if($check){
            $goodsService->delFav($id, $uid);
            $goodsService->favNumDecre($id);
            return $this->responseSuccess(["fav"=>0], "取消收藏成功!");
        }else{
            $goodsService->addFav($id, $uid);
            $goodsService->favNumIncre($id);
            return $this->responseSuccess(["fav"=>1],"收藏成功!");
        }
    }


    public function doClearFailureAction(GoodsService $goodsService){
        $uid = $this->getUid();
        $goodsService->clearFailure($uid);
        return $this->responseMsgReload("操作成功!");
    }

}
